package com.project.frba.bifront.client.view.charts;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.layout.FitLayout;
import com.project.frba.bifront.client.model.Cube;
import com.project.frba.bifront.client.model.PieChartInformation;
import com.project.frba.bifront.client.rpc.ChartServices;
import com.project.frba.bifront.client.rpc.ChartServicesAsync;

public class PieChartConfigurationWindow extends Composite{

	private Window mainWindow = new Window("Configurar Grafico de Torta",390,150, true, false);
	private String descriptorName;
	private Button btAccept = new Button("Aceptar");
	private Button btCancel = new Button("Cancelar");
	private Cube cube;
	private TextBox txTitle = new TextBox();
	private Checkbox cbxIncludeLegend = new Checkbox();
	private Checkbox cbxColumnLayout = new Checkbox();
	private ChartServicesAsync chartServices = (ChartServicesAsync)GWT.create(ChartServices.class);
	
	
	public PieChartConfigurationWindow(Cube cube, String descriptorName){
		Panel windowPanel = new Panel();
		Label lbTitle = new Label("Titulo");
		Label lbIncludeLegend = new Label("Incluir leyenda");
		Label lbColumnLayout = new Label("Base Columna");
		this.cube = cube;
		this.descriptorName = descriptorName;
		
		txTitle.setWidth("250px");
		
		FlexTable layoutTable = new FlexTable();
		layoutTable.setWidget(0, 0, lbTitle);
		layoutTable.getCellFormatter().setWordWrap(0, 0, false);
		layoutTable.setWidget(0, 1, txTitle);
		layoutTable.setWidget(1, 0, lbIncludeLegend);
		layoutTable.getCellFormatter().setWordWrap(1, 0, false);
		layoutTable.setWidget(1, 1, cbxIncludeLegend);
		layoutTable.setWidget(2, 0, lbColumnLayout);
		layoutTable.getCellFormatter().setWordWrap(2, 0, false);
		layoutTable.setWidget(2, 1, cbxColumnLayout);
		loadActions();
		windowPanel.add(layoutTable);
		windowPanel.setHeight("100%");
		windowPanel.setWidth("100%");
		mainWindow.add(windowPanel);
		mainWindow.setLayout(new FitLayout());
		mainWindow.setButtons(new Button[]{btAccept, btCancel});
		initWidget(mainWindow);
		mainWindow.show();
	}
	
	private void loadActions(){
		btAccept.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				super.onClick(button, e);
				final PieChartInformation info = new PieChartInformation();
				info.setColLayout(cbxColumnLayout.getValue());
				info.setTitle(txTitle.getText());
				info.setIncludeLegend(cbxIncludeLegend.getValue());
				info.setSql(cube.getSql());
				info.setColumnPropertyName(cube.getColumnLeafPropertyName());
				info.setRowPropertyName(cube.getRowLeafPropertyName());
				info.setDescriptorName(descriptorName);
				
				AsyncCallback<String> chartCallback = new AsyncCallback<String>(){
					public void onSuccess(String result) {
						if(result != null){
							new ChartWindow(result, info.toString());
							mainWindow.close();
						}
					}
					public void onFailure(Throwable caught) {
						com.google.gwt.user.client.Window.alert("RPC FAILED CHART SERVICES");
						
					}
				};
				
				chartServices.doPieChart(info, chartCallback);
				
			}
		});
		
		btCancel.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				mainWindow.close();
			}
		});
		
	}
	
}
